// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
	// POST --> 新增部门
	// GET  --> 新增小组
	async adddepartmentAction(){
		let data = this.post();
		data.floor = 2;
		data.pid = this.shop_userinfo.departmentid;
		let orcode = await this.model('or_department').where({id:data.pid}).getField('orcode','one');
		data.orcode = orcode[0];
		let res = await this.model('or_department').add(data);
		return res?this.success({info:"添加成功！"}):this.fail('添加失败！');
	}

	async addgroupAction(){
		let data = this.post();
		if (!data.name) {
			return this.fail('名字填写不正确！')
		}
		data.floor = 3;
		let orcode = await this.model('or_department')
			.where({id:this.shop_userinfo.departmentid})
			.getField('orcode','one'); 
		data.orcode = orcode[0];
		let res = await this.model('or_department').add(data);
		return res?this.success({info:"添加成功！"}):this.fail('添加失败！');
	}

	async departmentlistAction(){
		let search = this.post('search');

		let shopid = this.shop_userinfo.departmentid;

		let alist = await this.model('or_department')
				.where({pid: shopid, 'name': ['like', '%'+search+'%']})
				.select();

		let child = [];

		if (alist.length>0) {
			let xzPid = [];
			for (var i = 0; i < alist.length; i++) {
				xzPid.push(alist[i].id)
			}

			child = await this.model('or_department').where({pid: ['IN', xzPid]}).select()
		}

		let data = {
			blist: alist,
			xlist: child
		}

		return this.json(data);
	}

	async editdepartmentAction(){
		if(this.isPost){
			let data = this.post();
			let res = await this.model('or_department').where({id:data.id}).update(data);
			return res?this.success({info:"编辑成功！"}):this.fail('编辑失败！');
		}
		let id = this.get('id');
		let ainfo = await this.model('or_department').where({id:id}).find();
		return this.json(ainfo);
	}

	async deletedepartmentAction(){
		let id = this.post('id');
		//有下级不能删除
		let childids = await this.model('or_department').where({pid:id}).find();
		if(!think.isEmpty(childids)){
			return this.fail('请先删除该部门下小组！');
		}
		//判断有没有人员归属
		let haveuser = await this.model('or_app_user').where({departmentid:id, isdelete: 0}).find();
		if(!think.isEmpty(haveuser)){
			return this.fail('该分类下有人员存在不能删除！');
		}
		let res = await this.model('or_department').where({id:id}).delete();
		return res?this.success({info:"删除成功！"}):this.fail('删除失败！');
	} 

	async departmentchangeAction(){
		let data = this.post();
		let gstaff = await this.model('or_department')
				.alias('a')
				.field('u.id')
				.join('think_or_department g on g.pid=a.id')
				.join('think_or_app_user u on u.departmentid=g.id')
				.where({'a.id':data.id,'a.isdisable':0,'u.isdisable':0})
				.select()
		let dstaff = await this.model('or_department')
				.alias('d')
				.field('u.id')
				.join('think_or_app_user u on u.departmentid=d.id')
				.where({'d.id':data.id,'u.isdisable':0})
				.select()
		if(dstaff.length === 0 && gstaff.length === 0){ 
			//判断传进来的是小组还是部门
			let findFloor = await this.model('or_department').where({id:data.id}).getField('floor')
			//如果是部门，停用部门下小组也停用
			if(findFloor[0] === 2){
				let gId = await this.model('or_department')
							.alias('d')
							.field('o.id')
							.join('`think_or_department` o on o.pid = d.id')
							.where({'d.id':data.id})
							.select()
				if(data.isdisable == 1){ 
					for (var i = 0; i < gId.length; i++) {
						let res = await this.model('or_department').where({id:gId[i].id}).update({isdisable:data.isdisable});
					}
				} 
				let res = await this.model('or_department').where({id:data.id}).update({isdisable:data.isdisable});
				return res?this.success({info:'更改成功'}):this.fail('更改失败');
				
			}else{
				//如果是小组，启用的话，先判断当前部门是否正常
				let dIsdisable = await this.model('or_department')
							.alias('d')
							.field('o.isdisable')
							.join('`think_or_department` o on o.id = d.pid')
							.where({'d.id':data.id})
							.find()
				if(dIsdisable.isdisable === 0){
					let res = await this.model('or_department').where({id:data.id}).update({isdisable:data.isdisable});
					return res?this.success({info:'更改成功'}):this.fail('更改失败');
				}else{
					return this.fail('请先启用相关部门，然后启用该小组！')
				}
			}
		}else{
			return this.fail('该部门下尚有员工，不可执行该操作！')
		}
	}

	async getshopmanageAction(){
		let did = this.shop_userinfo.departmentid;
		let manage = await this.model('or_app_user').where({departmentid:did,ismanage:1}).find();
		return this.json(manage);
	}
}